package com.mofum.scope.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mofum.scope.annotation.controller.QueryScope;
import com.mofum.scope.annotation.controller.ServiceColumn;
import com.mofum.scope.annotation.controller.UpdateScope;
import com.mofum.scope.dto.UserDto;
import com.mofum.scope.entity.Page;
import com.mofum.scope.entity.User;
import com.mofum.scope.entity.input.DpRoleMenuPageInput;
import com.mofum.scope.service.IUserService;

/**
 * @author developer@omuao.com
 * @since 2019-03-28
 **/
@RestController
@RequestMapping("/user")
public class UserController extends ScopeController {

    public static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    IUserService         userService;

    @RequestMapping("/add")
    public Object addUser(User user) {
        userService.addUser(user);
        return "SUCCESS";
    }

    @RequestMapping("/query")
    public Object query(UserDto userDto) {
        logger.info(userDto.toString());
        return userService.queryUser(userDto, Page.PAGE_ONE);
    }

    @RequestMapping("/scope/query")
    @QueryScope
    public Object scopeQuery(UserDto userDto) {
        logger.info(userDto.toString());
        return userService.queryUser(userDto, Page.PAGE_ONE);
    }

    /**
     * 不支持
     * Mybatis-plus 分页 直接调用mybatisplus的分页
     * ObjectUtils.map2Object(..) 函数不支持QueryWrapper以及LambdaQueryWrapper
     * ObjectUtils中的 Set<String> set = data.keySet(); #set#中的值是ew,page,param1,param2...
     * @param page
     * @param userDto
     * @return
     */
    @GetMapping("/scope/getUserPage")
    @QueryScope
    public Object getUserPage(com.baomidou.mybatisplus.extension.plugins.pagination.Page page,
                              User userDto) {
        return userService.page(page, Wrappers.query(userDto));
    }

    /**
     * 不支持
     * Mybatis-plus 分页 调用自己的service重新组装参数在调用mybatisplus的分页
     * ObjectUtils.map2Object(..) 函数不支持QueryWrapper以及LambdaQueryWrapper的转换
     * ObjectUtils中的 Set<String> set = data.keySet(); #set#中的值是ew,page,param1,param2...
     * @param page
     * @param userDto
     * @return
     */
    @GetMapping("/scope/getUserPage2")
    @QueryScope
    public Object getUserPage(com.baomidou.mybatisplus.extension.plugins.pagination.Page page,
                              DpRoleMenuPageInput input) {

        return userService.dpUserPage(page, input);

    }

    @RequestMapping("/del")
    @UpdateScope(columns = { @ServiceColumn(value = "serviceIds"),
            @ServiceColumn(value = "ids", type = "P_SHOP", array = true, json = true) })
    public Object del(UserDto userDto, String ids) {
        logger.info(userDto.toString());
        return "SUCCESS";
    }
}
